﻿<?php
include_once("../initial.php");
include_once("../dbdriver/channelWHandle.php");
include_once("../dbdriver/channelRHandle.php");
class channelLogic
{
    public function insert_channel($array)
    {
        if(is_array($array)==false)
        {
            return false;
        }
        if(count($array)==0)
        {
            return false;
        }
        $whandle = new channelWHandle();
        return $whandle->insert_data($array);
    }
    public function update_channel($id, $name, $info)
    {
    		if(trim($id)=="")
    		{
    				return false;
    		}
    		$cw = new channelWHandle();
    		$array=array("name"=>$name, "info"=>$info);
    		return $cw->update_channel($array, $id);
    }
    public function get_channel($array='',$orderby="",$limit="")
    {
        $rhandle = new channelRHandle();
        if($array == '')
        {
            return $rhandle->get_all_data($orderby,$limit);
        }
        else
        {
            return $rhandle->get_some_data($array,$orderby,$limit);
        }
    }
    public function get_channel_num($array='')
    {
        $rhandle = new channelRHandle();
        return $rhandle->get_channel_num($array);
    }

    public function delete_channel($id)
    {
        $handle = new channelWHandle();
        return $handle->delete_channel($id);
    }
    
    //channel-user
    public function add_user($channelId, $uid)
    {
    		if($channelId=='' || $uid=="")
    		{
    				return false;
    		}
    		$handle = new channelWHandle();
    		return $handle->insert_channel_user($channelId, $uid);
    }
    public function get_users($channelId)
    {
    		if($channelId=="")
    		{
    				return false;
    			}
    		$rhandle = new channelRHandle();
    		return $rhandle->get_users($channelId);
    }
    public function remove_user($channelId, $userId)
    {
    		if($channelId=="" || $userId=="")
    		{
    				return  false;
    		}
    		$whandle = new channelWHandle();
    		return $whandle->delete_channel_user($channelId, $userId);
    }
    public function containsUser($channelId, $uid)
    {
    		if($channelId=="" || $uid=="")
    		{
    				return  false;
    		}
    		$rhandle = new channelRHandle();
    		$count = $rhandle->count_channel_user($channelId, $uid);
    		if($count>0)
    		{
    				return true;
    			}
    		return false;
    }
    
    //channel-device
    public function add_device($channelId, $did)
    {
    		if($channelId=='' || $did=="")
    		{
    				return false;
    		}
    		$handle = new channelWHandle();
    		return $handle->insert_channel_device($channelId, $did);
    }
    public function get_devices($channelId)
    {
    		if($channelId=="")
    		{
    				return false;
    			}
    		$rhandle = new channelRHandle();
    		return $rhandle->get_devices($channelId);
    }
    public function remove_device($channelId, $did)
    {
    		if($channelId=="" || $did=="")
    		{
    				return  false;
    		}
    		$whandle = new channelWHandle();
    		return $whandle->delete_channel_device($channelId, $did);
    }
    public function containsDevice($channelId, $did)
    {
    		if($channelId=="" || $did=="")
    		{
    				return false;
    		}
    		$rhandle = new channelRHandle();
    		$count = $rhandle->count_channel_device($channelId, $did);
    		if($count>0)
    		{
    				return true;
    			}
    		return false;
    }
    public function searchByKeyword($keyword)
    {
    		if(strlen($keyword)<3)
    		{
    				return array();
    		}
    		$cr = new channelRHandle();
    		return $cr->searchByKeyword($keyword, "limit 0, 30");
    }
}
?>
